<template>
  <div class="bigbox">
    <!-- 表头盒子 -->
    <!-- 盒子主体 -->
    <div class="mainbox">
      <div class="top">
        <div class="search">
          <el-select
            v-model="selectValue"
            @change="change"
            style="width: 100%; margin-right: 20px"
          >
            <el-option
              v-for="item in comboList"
              :key="item.value + item.text"
              :label="item.text"
              :value="item.text"
            >
            </el-option>
          </el-select>
          <el-input
            placeholder="请输入关键字搜索"
            prefix-icon="el-icon-search"
            v-model="inputValue"
          >
          </el-input>
        </div>
        <div class="towBtn">
          <button @click="chongzhi" class="button">重置</button>
          <button @click="chaxun" class="button chaxun">查询</button>
        </div>
      </div>
      <div class="bodybox">
        <!-- 新增按钮 -->
        <!-- 循环图片盒子 -->
        <div class="table" v-if="domainType !== 'Root'">
          <div class="titlebox">
            <div class="text"></div>
            <div
              class="right"
              :style="{ alignItems: 'center', lineHeight: '32.5px' }"
            >
              <button
                type="primary"
                size="small"
                class="bewdao daochu"
                @click="view"
              >
                预览
              </button>
              <button
                type="primary"
                size="small"
                class="bewdao daochu"
                @click="sync"
              >
                同步
              </button>
              <button
                type="primary"
                size="small"
                class="bewdao daochu"
                @click="copyDialog"
              >
                复制
              </button>
              <el-tooltip
                class="item"
                effect="dark"
                content="刷新"
                placement="top"
              >
                <span
                  @click="shuaxin"
                  :style="{ cursor: 'pointer', fontSize: '20px' }"
                  ><i class="el-icon-refresh-right"></i
                ></span>
              </el-tooltip>
              <el-dropdown trigger="click" @command="command">
                <span class="el-dropdown-link">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="密度"
                    placement="top"
                  >
                    <span :style="{ cursor: 'pointer', fontSize: '20px' }"
                      ><i class="el-icon-d-caret"></i
                    ></span>
                  </el-tooltip>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item class="dropdown-list" command="默认"
                    >默认</el-dropdown-item
                  >
                  <el-dropdown-item class="dropdown-list" command="中等"
                    >中等</el-dropdown-item
                  >
                  <el-dropdown-item class="dropdown-list" command="紧凑"
                    >紧凑</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
          <el-table
            v-loading="loading"
            :data="tableList"
            style="width: 100%"
            border
            height="400px"
            id="exportTabyong"
            ref="table"
            :size="iissize"
            :header-cell-style="{
              background: '#f5f5f5',
              'text-align': 'center',
            }"
          >
            <template slot="empty">
              <el-empty description="暂无数据"></el-empty>
            </template>
            <!-- <el-table-column
                align="center"
                type="index"
                width="50"
                label="序号"
              >
              </el-table-column> -->
            <el-table-column prop="panelName" label="视图面板">
            </el-table-column>
            <el-table-column prop="componentName" label="组件编码">
            </el-table-column>
            <el-table-column prop="componentLabel" label="组件名称">
            </el-table-column>
            <el-table-column
              align="center"
              width="300px"
              prop="componentType"
              label="组件类型"
            >
            </el-table-column>
            <el-table-column
              width="100px"
              align="center"
              prop="readOnly"
              label="是否只读"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.readOnly ? "是" : "否" }}</span>
              </template>
            </el-table-column>
            <el-table-column
              width="100px"
              align="center"
              prop="hidden"
              label="是否隐藏"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.hidden ? "是" : "否" }}</span>
              </template>
            </el-table-column>
            <el-table-column width="300px" align="center" label="操作">
              <template slot-scope="scope">
                <div class="table-cz">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="编辑"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-edit"
                      @click="edit(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="删除"
                    placement="top"
                  >
                    <i
                      style="font-size: 14px; color: red; cursor: pointer"
                      class="el-icon-delete"
                      @click="del(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="上移"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-upload2"
                      @click="move('up', scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="下移"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-download"
                      @click="move('down', scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="配置分发"
                    placement="top"
                  >
                    <img
                      style="width: 14px; height: 14px; cursor: pointer"
                      src="../../../assets/image/ff.png"
                      @click="Distribute(scope.row)"
                  /></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="事件管理"
                    placement="top"
                  >
                    <img
                      style="width: 14px; height: 14px; cursor: pointer"
                      src="../../../assets/image/sjgl.png"
                      @click="Event(scope.row)"
                  /></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="校验管理"
                    placement="top"
                  >
                    <img
                      style="width: 14px; height: 14px; cursor: pointer"
                      src="../../../assets/image/jy.png"
                      @click="Validate(scope.row)"
                  /></el-tooltip>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <!-- <el-pagination
              background
              class="pagination"
              layout="prev, pager, next"
              :current-page.sync="pageobj._pageNo"
              @current-change="chaxun"
              :total="total"
            >
            </el-pagination> -->
        </div>
        <!-- 新增/修改弹框 -->
        <el-dialog
          :show-close="true"
          :visible.sync="copyVisible"
          v-if="copyVisible"
          width="1000px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          append-to-body
          :before-close="diaClose"
          :center="true"
        >
          <copy-dialog @diaClose="diaClose" :data="viewData"></copy-dialog>
        </el-dialog>
        <el-dialog
          :show-close="true"
          :visible.sync="editVisible"
          v-if="editVisible"
          width="500px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          append-to-body
          :before-close="diaClose"
          :center="true"
        >
          <EditDialog
            @diaClose="diaClose"
            :rowData="rowData"
            @updateList="chaxun()"
          ></EditDialog>
        </el-dialog>
        <el-dialog
          :show-close="true"
          :visible.sync="DistributeVisible"
          v-if="DistributeVisible"
          width="1000px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :before-close="diaClose"
          append-to-body
          :center="true"
        >
          <distribute-dialog
            @diaClose="diaClose"
            :DistributeData="DistributeData"
          ></distribute-dialog>
        </el-dialog>
        <el-dialog
          :show-close="true"
          :visible.sync="EventVisible"
          v-if="EventVisible"
          width="1000px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :before-close="diaClose"
          append-to-body
          :center="true"
        >
          <event-dialog
            @diaClose="diaClose"
            :EventData="EventData"
          ></event-dialog>
        </el-dialog>
        <el-dialog
          :show-close="true"
          :visible.sync="ValidateVisible"
          v-if="ValidateVisible"
          width="1000px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :before-close="diaClose"
          append-to-body
          :center="true"
        >
          <validata-dialog
            @diaClose="diaClose"
            :ValidData="ValidData"
          ></validata-dialog>
        </el-dialog>
        <el-dialog
          :show-close="true"
          :visible.sync="viewVisible"
          v-if="viewVisible"
          width="1000px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :before-close="diaClose"
          append-to-body
          :center="true"
        >
          <LedgerView v-if="viewObj.name" :viewObj="viewObj"></LedgerView>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getDatabaseListApi,
  getParentListApi
} from '@/api/Device-all/Device-model'
import {
  delModelViewApi,
  downModelViewApi,
  getComboListApi,
  getComponentListApi,
  getPageViewDataApi,
  syncModelViewApi,
  upModelViewApi
} from '@/api/Device-all/Device-view'
import EditDialog from './components/EditDialog/EditDialog.vue'
import CopyDialog from './components/CopyDialog.vue'
import DistributeDialog from './components/DistributeDialog.vue'
import EventDialog from './components/EventDialog/EventDialog.vue'
import ValidataDialog from './components/ValidataDialog/ValidataDialog.vue'
import LedgerView from '@/views/Ledger-view/index.vue'
export default {
  name: 'DeviceView',
  components: {
    EditDialog,
    CopyDialog,
    DistributeDialog,
    EventDialog,
    ValidataDialog,
    LedgerView
  },
  props: {
    viewData: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      comboList: [],
      selectValue: '',
      rowData: {},
      copySelectValue: '',
      storageWidth: '500px',
      imageUrl: '',
      filterText: '',
      changeValue: '',
      inputValue: '',
      domainId: '',
      domainType: '',
      parentId: '',
      node: null,
      diaTime: '',
      timeType: true,
      tableList: [{}],
      loading: false,
      iissize: 'medium',
      classData: {},
      storageData: {},
      relationData: {},
      DistributeData: {},
      EventData: {},
      ValidData: {},
      dialogForm: {},
      parentList: [],
      databaseList: [],
      deflist: [],
      props: {
        label: 'domainName'
      },
      copyVisible: false,
      DistributeVisible: false,
      editVisible: false,
      EventVisible: false,
      ValidateVisible: false,
      viewVisible: false,
      isedit: 'add',
      // pageobj: {
      //   _pageNo: 0,
      //   _pageSize: 15,
      // },
      // total: 0,
      viewObj: {
        id: '0',
        name: ''
      }
    }
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    }
  },
  mounted () {
    this.getComboList()
    this.getComponentList(this.viewData.classId)
  },
  methods: {
    // 查询设备模型视图面板下拉框选项列表
    async getComboList () {
      const res = await getComboListApi(this.viewData.classId)
      this.comboList = res.data.list
    },
    // 查询设备模型视图组件列表
    async getComponentList (id) {
      this.loading = true
      const res = await getComponentListApi(id)
      this.deflist = res.data.list
      this.tableList = res.data.list
      this.loading = false
    },
    async getDatabaseList () {
      const res = await getDatabaseListApi()
      this.databaseList = res.data.list
    },
    // 复制
    copyDialog () {
      this.copyVisible = true
    },

    // 同步
    sync () {
      this.$confirm('请确认是否进行视图同步操作？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await syncModelViewApi({
            classId: this.viewData.classId
          })
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.message,
              duration: 2000
            })
            this.getComponentList(this.viewData.classId)
          } else {
            this.$message({
              type: 'error',
              message: res.message,
              duration: 2000
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async getParentList (id) {
      const res = await getParentListApi(id)
      this.parentList = res.data.list
    },
    async view () {
      const res = await getPageViewDataApi(this.viewData.classId)
      // window.open(`/static/pio/call-police/userscreen.html#/Ledger-view/0/${res.data.entity.pageCode}`)
      // this.viewObj.name = res.data.entity.pageCode
      this.$set(this.viewObj, 'name', res.data.entity.pageCode)
      console.log(this.viewObj)
      this.viewVisible = true
    },
    // 新增弹窗
    submit (val) {
      this.isedit = val
      if (this.isedit === 'add') {
        // this.dialogForm.extendGroup = false
        // this.dialogForm.canInstance = false
        // this.dialogForm.domainId = this.domainId
        this.$set(this.dialogForm, 'extendGroup', false)
        this.$set(this.dialogForm, 'canInstance', false)
        this.$set(this.dialogForm, 'domainId', this.domainId)
      }
      this.getParentList(this.domainId)
      this.dialogVisible = true
    },
    // 分发弹窗
    Distribute (item) {
      console.log(item)
      this.DistributeData = item
      this.DistributeVisible = true
    },
    // 事件管理
    async Event (item) {
      this.EventData = item
      this.EventVisible = true
    },
    // 校验管理
    Validate (item) {
      this.ValidData = item
      this.ValidateVisible = true
    },
    diaClose () {
      this.copyVisible = false
      this.editVisible = false
      this.DistributeVisible = false
      this.EventVisible = false
      this.ValidateVisible = false
      this.viewVisible = false
      this.dialogForm = {}
    },
    shuaxin () {
      this.getComponentList(this.viewData.classId)
      this.$message({
        type: 'success',
        message: '列表刷新成功',
        duration: 2000
      })
    },
    async change (val) {
      console.log(val)
      this.changeValue = val
      // await this.getComponentList(this.viewData.classId)
      // const list = [...this.tableList]
      // this.tableList = list.filter((item) =>
      //   item.panelName.includes(this.changeValue)
      // )
      // const list = [...this.deflist]
      // if (this.inputValue) {
      //   this.tableList = list.filter(
      //     (item) =>
      //       item.componentLabel.includes(this.inputValue) ||
      //       item.componentName.includes(this.inputValue)
      //   )
      // } else {
      //   this.tableList = [...list]
      // }
    },
    // 查询分页信息
    async chaxun () {
      await this.getComponentList(this.viewData.classId)
      const list = [...this.deflist]
      if (this.inputValue && !this.changeValue) {
        this.tableList = list.filter(
          (item) =>
            item.componentLabel.includes(this.inputValue) ||
            item.componentName.includes(this.inputValue)
        )
      } else if (this.changeValue && !this.inputValue) {
        this.tableList = list.filter((item) =>
          item.panelName.includes(this.changeValue)
        )
      } else if (this.changeValue && this.inputValue) {
        this.tableList = list.filter(
          (item) =>
            (item.componentLabel.includes(this.inputValue) ||
              item.componentName.includes(this.inputValue)) &&
            item.panelName.includes(this.changeValue)
        )
      } else {
        this.tableList = [...list]
      }
    },
    command (val) {
      if (val === '默认') {
        this.iissize = 'medium'
      } else if (val === '中等') {
        this.iissize = 'small'
      } else {
        this.iissize = 'mini'
      }
    },
    // 移动
    async move (type, item) {
      this.$confirm('此操作将移动视图模型组件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res =
            type === 'up'
              ? await upModelViewApi(item.componentId)
              : await downModelViewApi(item.componentId)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.message,
              duration: 2000
            })
            this.getComponentList(this.viewData.classId)
          } else {
            this.$message({
              type: 'error',
              message: res.message,
              duration: 2000
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消移动'
          })
        })
    },
    // 修改弹窗
    async edit (item) {
      this.isedit = 'edit'
      console.log(item)
      this.rowData = item
      // const res = await getClassDataApi(item.classId)
      // const data = res.data.entity
      // this.dialogForm = {
      //   classCode: data.classCode,
      //   className: data.className,
      //   classImage: data.classImageBase64,
      //   classId: data.classId,
      //   classExplain: data.classExplain
      // }
      this.editVisible = true
    },
    // 删除
    async del (item) {
      this.$confirm('此操作将删除该模型视图组件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            const res = await delModelViewApi(item.componentId)
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.message,
                duration: 2000
              })
              this.getComponentList(this.viewData.classId)
            } else {
              this.$message({
                type: 'error',
                message: res.message,
                duration: 2000
              })
            }
          } catch (e) {
            console.log(e)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    chongzhi () {
      this.inputValue = ''
      this.getComponentList(this.viewData.classId)
    }
  }
}
</script>

<style lang="scss" scoped>
.table-cz {
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.dialog-footer {
  display: flex;
  justify-content: center;
}
.bigbox {
  background-color: #f1f1f1;
  width: 100%;
  padding: 10px;
  .mainbox {
    display: flex;
    flex-direction: column;
    width: 100%;
    .top {
      padding: 0 40px;
      display: flex;
      justify-content: space-between;
      width: 100%;
      min-height: 80px;
      background-color: #fff;
      border-bottom: 1px solid #f1f1f1;
      margin-bottom: 10px;
      align-items: center;
      .search {
        display: flex;
        align-items: center;
        width: 600px;
      }
    }
    .bodybox {
      flex: 1;
      position: relative;
      background-color: #fff;
      padding: 20px;
      padding-bottom: 30px;
      //   min-height: 88.7vh;
      .pagination {
        position: absolute;
        right: 20px;
        bottom: 10px;
      }
      .table {
        .titlebox {
          display: flex;
          justify-content: space-between;
          margin-bottom: 10px;
          .text {
            font-size: 16px;
          }
          .right {
            align-items: center;
            i {
              margin-left: 10px;
            }
            .bewdao {
              height: 30px;
              padding: 0 20px;
              margin-right: 10px;
            }
            .daochu {
              background-color: #1890ff;
              color: #fff;
              border: none;
              border-radius: 3px;
            }
          }
        }
      }
      .dialog {
        .title {
          font-size: 28px;
          margin-bottom: 30px;
        }
        .text {
          font-size: 12px;
          color: #3b8eea;
        }
      }
    }
  }
}
::v-deep textarea {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}
:v-deep.el-table th.gutter {
  display: none;
  width: 0;
}
::v-deep.el-table colgroup col[name="gutter"] {
  display: none;
  width: 0;
}
::v-deep.el-table__body {
  width: 100% !important;
}
// ::v-deep .el-select {
//   width: 100%;
// }
// ::v-deep .el-input {
//   height: 32px;
// }
::v-deep .el-input__inner {
  height: 32px;
}
::v-deep .el-input__icon {
  line-height: 1;
}

.el-form-item {
  margin-bottom: 20px;
}
.button {
  width: 60px;
  height: 30px;
  margin-right: 5px;
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 3px;
}
.chaxun {
  background-color: #1890ff;
  color: #fff;
}

.dropdown-list {
  padding: 0;
  text-align: center;
  width: 50px;
}
</style>
